package com.ruihu.api.module.business.task.controller;

import com.ruihu.admin.common.anno.NoNeedLogin;
import com.ruihu.admin.common.anno.NoValidPrivilege;
import com.ruihu.admin.common.anno.OperateLog;
import com.ruihu.admin.common.domain.ResponseDTO;
import com.ruihu.admin.constant.SwaggerTagConst;
import com.ruihu.admin.module.system.login.LoginResponseCodeConst;
import com.ruihu.admin.module.system.login.LoginService;
import com.ruihu.admin.module.system.login.domain.LoginDetailVO;
import com.ruihu.admin.module.system.login.domain.RequestTokenBO;
import com.ruihu.admin.module.system.user.domain.dto.UserLoginFormDTO;
import com.ruihu.admin.util.RequestTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@Api(tags = {SwaggerTagConst.Mobile.TASK_API})
@OperateLog
public class LoginConroller {

    @Autowired
    private LoginService loginService;

    @PostMapping("/api/session/login")
    @ApiOperation(value = "登录", notes = "登录")
    @NoNeedLogin
    public ResponseDTO<LoginDetailVO> login(@Valid @RequestBody UserLoginFormDTO loginForm, HttpServletRequest request) {
        return loginService.login(loginForm, request);
    }

    @GetMapping("/api/session/logOut")
    @ApiOperation(value = "退出登陆", notes = "退出登陆")
    @NoValidPrivilege
    public ResponseDTO<Boolean> logOut() {
        RequestTokenBO requestToken = RequestTokenUtil.getRequestUser();
        if (null == requestToken) {
            return ResponseDTO.wrap(LoginResponseCodeConst.LOGIN_ERROR);
        }
        return loginService.logoutByToken(requestToken);
    }


}
